import hmac
import hashlib
import base64
import time
import requests
import json
import datetime
# 设置签名
def gen_sign(timestamp, secret):
    # 拼接timestamp和secret
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()
    # 对结果进行base64处理
    sign = base64.b64encode(hmac_code).decode('utf-8')
    return sign
# 发送飞书文本消息(@所有人)
def send_lark_text_message(webhook, secret, content):
    signature = gen_sign(int(time.time()), secret)
    headers = {
        "Content-Type": "application/json"
    }
    payload = {
        "timestamp": int(time.time()),
        "sign": signature,
        "msg_type": "text",
        "content": {
                "text": content+"<at user_id=\"all\">所有人</at>"
        }
    }
    response = requests.post(webhook, headers=headers, data=json.dumps(payload))
    if "success" in response.text:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送成功")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  post信息：{response.text}")
    else:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送失败，状态码：{response.status_code}")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  错误信息：{response.text}")

# 发送卡片消息到飞书机器人
def send_flybook_card(webhook, total_list,message_list,secret):
    '''
    webhook: web地址
    total_list: 总体数据列表
    message_list: 详细数据列表
    secret: 签名验证
    '''
    headers = {
        "Content-Type": "application/json"
    }
    # 表格卡片的JSON代码
    card_data = {
    "schema": "2.0",
    "config": {
        "update_multi": True
    },
    "body": {
        "direction": "vertical",
        "padding": "12px 12px 12px 12px",
        "elements": [
            {
                "tag": "table",
                "columns": [
                    {
                        "data_type": "text",
                        "name": "col_4j5vk040i8k",
                        "display_name": "更新时间",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_name",
                        "display_name": "主体",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_scale",
                        "display_name": "总消耗",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_arr",
                        "display_name": "Δ总消耗",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_4j5vk032i7l",
                        "display_name": "转化数",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_w0xrt90kluk",
                        "display_name": "Δ转化数",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_hp2cvl5rlke",
                        "display_name": "CPA",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_crqfetc5tq",
                        "display_name": "ΔCPA",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_2ovmac5v06n",
                        "display_name": "ROSE",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_12nt9ko8kaz",
                        "display_name": "ΔROSE",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_kd2gavnshnb",
                        "display_name": "CTR",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_94gre8bx6d9",
                        "display_name": "ΔCTR",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_mut4v893ml",
                        "display_name": "CVR",
                        "horizontal_align": "left",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_msykpigl6oq",
                        "display_name": "ΔCVR",
                        "horizontal_align": "left",
                        "width": "auto"
                    }
                ],
                "rows": total_list,
                "row_height": "middle",
                "header_style": {
                    "background_style": "none",
                    "bold": True,
                    "lines": 1
                },
                "page_size": 5,
                "margin": "0px 0px 0px 0px"
            },
            {
                "tag": "table",
                "columns": [
                    {
                        "data_type": "text",
                        "name": "col_2cqmiporlj6",
                        "display_name": "主体",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_name",
                        "display_name": "账户名称",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_scale",
                        "display_name": "账户ID",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "customer_arr",
                        "display_name": "数据更新时间",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_6g6c5oe343j",
                        "display_name": "总消耗",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_ppotysk1l7f",
                        "display_name": "Δ总消耗",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_dtee9ycd85s",
                        "display_name": "转化数",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_8vmk9p2xqff",
                        "display_name": "Δ转化数",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_6yeio6uxo6t",
                        "display_name": "CPA(转化成本)",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_40ebl7h73t5",
                        "display_name": "ΔCPA",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_83bzrvyq06k",
                        "display_name": "Rose(支付完成广告支出回报率_站内)",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_pns7w5xlki9",
                        "display_name": "ΔRose",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_ft3zf27u7sb",
                        "display_name": "CTR",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_ie9c9f7ttgo",
                        "display_name": "ΔCTR",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_xsoqqkac45e",
                        "display_name": "CVR",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    },
                    {
                        "data_type": "text",
                        "name": "col_zd3hywwsbxs",
                        "display_name": "ΔCVR",
                        "horizontal_align": "left",
                        "vertical_align": "center",
                        "width": "auto"
                    }
                ],
                "rows": message_list,
                "row_height": "low",
                "header_style": {
                    "background_style": "grey",
                    "bold": True,
                    "lines": 1
                },
                "page_size": 10,
                "margin": "0px 0px 0px 0px",
                "element_id": "LR_123456"
            }
        ]
    }
}
    payload = {
        "timestamp": int(time.time()),
        "sign": gen_sign(int(time.time()), secret),
        "msg_type": "interactive",
        "card": card_data
    }
    response = requests.post(webhook, headers=headers, data=json.dumps(payload))
    if  "success" in response.text:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送成功")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  post信息：{response.text}")
    else:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送失败，状态码：{response.status_code}")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  错误信息：{response.text}")

def send_flybook_card2(webhook, total_list,secret):
    '''
    webhook: web地址
    total_list: 总体数据列表
    message_list: 详细数据列表
    secret: 签名验证
    '''
    headers = {
        "Content-Type": "application/json"
    }
    # 表格卡片的JSON代码
    card_data = {
        "schema": "2.0",
        "config": {
            "update_multi": True
        },
        "body": {
            "direction": "vertical",
            "padding": "12px 12px 12px 12px",
            "elements": [
                {
                    "tag": "table",
                    "columns": [
                        {
                            "data_type": "text",
                            "name": "col_4j5vk040i8k",
                            "display_name": "更新时间",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_name",
                            "display_name": "账户名称",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_scale",
                            "display_name": "店铺名称",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_arr",
                            "display_name": "成本",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_4j5vk032i7l",
                            "display_name": "Δ成本",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_w0xrt90kluk",
                            "display_name": "订单数",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_hp2cvl5rlke",
                            "display_name": "Δ订单数",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_crqfetc5tq",
                            "display_name": "下单平均成本",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_2ovmac5v06n",
                            "display_name": "Δ下单平均成本",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_12nt9ko8kaz",
                            "display_name": "总收入",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_kd2gavnshnb",
                            "display_name": "Δ总收入",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_94gre8bx6d9",
                            "display_name": "ROI",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_mut4v893ml",
                            "display_name": "ΔROI",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                    ],
                    "rows": total_list,
                    "row_height": "middle",
                    "header_style": {
                        "background_style": "none",
                        "bold": True,
                        "lines": 1
                    },
                    "page_size": 5,
                    "margin": "0px 0px 0px 0px"
                }
            ]
        }
    }
    payload = {
        "timestamp": int(time.time()),
        "sign": gen_sign(int(time.time()), secret),
        "msg_type": "interactive",
        "card": card_data
    }
    response = requests.post(webhook, headers=headers, data=json.dumps(payload))
    if "success" in response.text:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送成功")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  post信息：{response.text}")
    else:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送失败，状态码：{response.status_code}")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  错误信息：{response.text}")


def send_flybook_card3(webhook, total_list,secret):
    '''
    webhook: web地址
    total_list: 总体数据列表
    message_list: 详细数据列表
    secret: 签名验证
    '''
    headers = {
        "Content-Type": "application/json"
    }
    # 表格卡片的JSON代码
    card_data = {
        "schema": "2.0",
        "config": {
            "update_multi": True
        },
        "body": {
            "direction": "vertical",
            "padding": "12px 12px 12px 12px",
            "elements": [
                {
                    "tag": "table",
                    "columns": [
                        {
                            "data_type": "text",
                            "name": "col_4j5vk040i8k",
                            "display_name": "更新时间",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_name",
                            "display_name": "账户名称",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_scale",
                            "display_name": "账户ID",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "customer_arr",
                            "display_name": "总消耗",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_4j5vk032i7l",
                            "display_name": "转化数",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_w0xrt90kluk",
                            "display_name": "CPA(转化成本)",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_hp2cvl5rlke",
                            "display_name": "Rose(支付完成广告支出回报率_站内)",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_crqfetc5tq",
                            "display_name": "CTR",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_2ovmac5v06n",
                            "display_name": "CPC",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_3ovmac9v96n",
                            "display_name": "点击量",
                            "horizontal_align": "left",
                            "width": "auto"
                        },
                        {
                            "data_type": "text",
                            "name": "col_7ovmac5v08n",
                            "display_name": "展示次数",
                            "horizontal_align": "left",
                            "width": "auto"
                        }
                    ],
                    "rows": total_list,
                    "row_height": "middle",
                    "header_style": {
                        "background_style": "none",
                        "bold": True,
                        "lines": 1
                    },
                    "page_size": 10,
                    "margin": "0px 0px 0px 0px"
                }
            ]
        }
    }
    payload = {
        "timestamp": int(time.time()),
        "sign": gen_sign(int(time.time()), secret),
        "msg_type": "interactive",
        "card": card_data
    }
    response = requests.post(webhook, headers=headers, data=json.dumps(payload))
    if "success" in response.text:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送成功")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  post信息：{response.text}")
    else:
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  消息发送失败，状态码：{response.status_code}")
        print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}  错误信息：{response.text}")
